package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {


    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 小程序登录
     *
     * @param code
     * @return
     */
    @Override
    public String wxLogin(String code) {
        Result<Long> result = driverInfoFeignClient.wxLogin(code);
        Long driverId = result.getData();

        // 生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        // 将token保存到redis中
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                driverId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.MINUTES);
        return token;
    }

    /**
     * 获取司机信息
     *
     * @return
     */
    @Override
    public DriverLoginVo getDriverLoginInfo() {
        Long driverId = AuthContextHolder.getUserId();
        Result<DriverLoginVo> result = driverInfoFeignClient.getDriverLoginInfo(driverId);
        return result.getData();
    }
}
